package net.sea.setup.ui.wizard.impl;

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;

import net.sea.commons.constants.AppConstants;
import net.sea.commons.util.DBUtils;
import net.sea.commons.util.StringUtils;
import net.sea.setup.config.custom.CustomConfig;
import net.sea.setup.config.custom.CustomConfigItem;
import net.sea.setup.ui.common.SelectFile;
import net.sea.setup.ui.wizard.AbstractWizardPage;
import net.sea.setup.ui.wizard.enums.OpEvent;
import net.sea.setup.ui.wizard.factory.DBFactory;
import net.sea.setup.ui.wizard.factory.db.DataBase;
import net.sea.setup.ui.wizard.factory.db.IDBType;

/**
 * 数据库配置页面
 * 
 * @author 华成伍
 *
 */
@SuppressWarnings("serial")
public class DBPage extends AbstractWizardPage {
	private JTextField ipTextField;
	private JTextField dbNameTextField;
	private JLabel dbNameLb = new JLabel("SID：");
	private JTextField dbUserTextField;
	private JPasswordField dbPasswordField;
	private JTextField dbURLTextField;
	private JTextField customDriverTextField = new JTextField();
	private JButton customDriverBtn = new JButton("\u6D4F\u89C8");
	private JTextField dbDriverClassTextField;
	private JComboBox dbTypeComboBox = new JComboBox();
	private JSpinner portSpinner = new JSpinner();
	private JCheckBox customDriverCheckBox = new JCheckBox(
			"\u81EA\u9009\u6570\u636E\u5E93\u9A71\u52A8\uFF1A");
	private JCheckBox initDBCheckBox = new JCheckBox(
			"\u521D\u59CB\u5316\u6570\u636E\u5E93");
	// 数据库连接测试信息
	private String testMsg = "";
	// 数据库对象
	private static DataBase db = new DataBase();

	public DBPage() {
		setOpaque(false);
		setBorder("\u914D\u7F6E\u6570\u636E\u5E93");
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 };
		gridBagLayout.columnWidths = new int[] { 20, 100, 300, 100, 0 };
		setLayout(gridBagLayout);

		JLabel dbTypeLb = new JLabel("\u6570\u636E\u5E93\u7C7B\u578B\uFF1A");
		GridBagConstraints gbc_dbTypeLb = new GridBagConstraints();
		gbc_dbTypeLb.gridwidth = 2;
		gbc_dbTypeLb.anchor = GridBagConstraints.EAST;
		gbc_dbTypeLb.insets = new Insets(0, 0, 5, 5);
		gbc_dbTypeLb.gridx = 0;
		gbc_dbTypeLb.gridy = 4;
		add(dbTypeLb, gbc_dbTypeLb);
		dbTypeComboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				initDB(OpEvent.ChanagedEvent);
			}
		});

		dbTypeComboBox.setModel(new DefaultComboBoxModel(
				AppConstants.SUPPORT_DB));
		GridBagConstraints gbc_dbTypeComboBox = new GridBagConstraints();
		gbc_dbTypeComboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_dbTypeComboBox.anchor = GridBagConstraints.NORTH;
		gbc_dbTypeComboBox.insets = new Insets(0, 0, 5, 5);
		gbc_dbTypeComboBox.gridx = 2;
		gbc_dbTypeComboBox.gridy = 4;
		add(dbTypeComboBox, gbc_dbTypeComboBox);

		JLabel dbIPLb = new JLabel("\u670D\u52A1\u5668IP\uFF1A");
		GridBagConstraints gbc_dbIPLb = new GridBagConstraints();
		gbc_dbIPLb.gridwidth = 2;
		gbc_dbIPLb.anchor = GridBagConstraints.EAST;
		gbc_dbIPLb.insets = new Insets(0, 0, 5, 5);
		gbc_dbIPLb.gridx = 0;
		gbc_dbIPLb.gridy = 5;
		add(dbIPLb, gbc_dbIPLb);

		ipTextField = new JTextField();
		ipTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				initDB(OpEvent.ModifyEvent);
			}
		});
		GridBagConstraints gbc_ipTextField = new GridBagConstraints();
		gbc_ipTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_ipTextField.anchor = GridBagConstraints.NORTH;
		gbc_ipTextField.insets = new Insets(0, 0, 5, 5);
		gbc_ipTextField.gridx = 2;
		gbc_ipTextField.gridy = 5;
		add(ipTextField, gbc_ipTextField);
		ipTextField.setColumns(10);

		JLabel dbPortLb = new JLabel("\u670D\u52A1\u5668\u7AEF\u53E3\uFF1A");
		GridBagConstraints gbc_dbPortLb = new GridBagConstraints();
		gbc_dbPortLb.gridwidth = 2;
		gbc_dbPortLb.anchor = GridBagConstraints.EAST;
		gbc_dbPortLb.insets = new Insets(0, 0, 5, 5);
		gbc_dbPortLb.gridx = 0;
		gbc_dbPortLb.gridy = 6;
		add(dbPortLb, gbc_dbPortLb);
		portSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				initDB(OpEvent.ModifyEvent);
			}
		});

		portSpinner.setModel(new SpinnerNumberModel(1, 1, 65535, 1));
		GridBagConstraints gbc_porSpinner = new GridBagConstraints();
		gbc_porSpinner.fill = GridBagConstraints.HORIZONTAL;
		gbc_porSpinner.anchor = GridBagConstraints.NORTH;
		gbc_porSpinner.insets = new Insets(0, 0, 5, 5);
		gbc_porSpinner.gridx = 2;
		gbc_porSpinner.gridy = 6;
		add(portSpinner, gbc_porSpinner);

		GridBagConstraints gbc_dbNameLb = new GridBagConstraints();
		gbc_dbNameLb.gridwidth = 2;
		gbc_dbNameLb.anchor = GridBagConstraints.EAST;
		gbc_dbNameLb.insets = new Insets(0, 0, 5, 5);
		gbc_dbNameLb.gridx = 0;
		gbc_dbNameLb.gridy = 7;
		add(dbNameLb, gbc_dbNameLb);

		dbNameTextField = new JTextField();
		dbNameTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				initDB(OpEvent.ModifyEvent);
			}
		});
		GridBagConstraints gbc_dbNameTextField = new GridBagConstraints();
		gbc_dbNameTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_dbNameTextField.anchor = GridBagConstraints.NORTH;
		gbc_dbNameTextField.insets = new Insets(0, 0, 5, 5);
		gbc_dbNameTextField.gridx = 2;
		gbc_dbNameTextField.gridy = 7;
		add(dbNameTextField, gbc_dbNameTextField);
		dbNameTextField.setColumns(10);

		JLabel dbUserLb = new JLabel("\u6570\u636E\u5E93\u7528\u6237\uFF1A");
		GridBagConstraints gbc_dbUserLb = new GridBagConstraints();
		gbc_dbUserLb.gridwidth = 2;
		gbc_dbUserLb.anchor = GridBagConstraints.EAST;
		gbc_dbUserLb.insets = new Insets(0, 0, 5, 5);
		gbc_dbUserLb.gridx = 0;
		gbc_dbUserLb.gridy = 8;
		add(dbUserLb, gbc_dbUserLb);

		dbUserTextField = new JTextField();
		GridBagConstraints gbc_dbUserTextField = new GridBagConstraints();
		gbc_dbUserTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_dbUserTextField.anchor = GridBagConstraints.NORTH;
		gbc_dbUserTextField.insets = new Insets(0, 0, 5, 5);
		gbc_dbUserTextField.gridx = 2;
		gbc_dbUserTextField.gridy = 8;
		add(dbUserTextField, gbc_dbUserTextField);
		dbUserTextField.setColumns(10);

		JLabel dbPasswordLb = new JLabel("\u6570\u636E\u5E93\u5BC6\u7801\uFF1A");
		GridBagConstraints gbc_dbPasswordLb = new GridBagConstraints();
		gbc_dbPasswordLb.gridwidth = 2;
		gbc_dbPasswordLb.anchor = GridBagConstraints.EAST;
		gbc_dbPasswordLb.insets = new Insets(0, 0, 5, 5);
		gbc_dbPasswordLb.gridx = 0;
		gbc_dbPasswordLb.gridy = 9;
		add(dbPasswordLb, gbc_dbPasswordLb);

		dbPasswordField = new JPasswordField();
		GridBagConstraints gbc_dbPasswordField = new GridBagConstraints();
		gbc_dbPasswordField.fill = GridBagConstraints.HORIZONTAL;
		gbc_dbPasswordField.anchor = GridBagConstraints.NORTH;
		gbc_dbPasswordField.insets = new Insets(0, 0, 5, 5);
		gbc_dbPasswordField.gridx = 2;
		gbc_dbPasswordField.gridy = 9;
		add(dbPasswordField, gbc_dbPasswordField);

		JLabel dbURLLb = new JLabel("\u6570\u636E\u5E93URL\uFF1A");
		GridBagConstraints gbc_dbURLLb = new GridBagConstraints();
		gbc_dbURLLb.gridwidth = 2;
		gbc_dbURLLb.anchor = GridBagConstraints.EAST;
		gbc_dbURLLb.insets = new Insets(0, 0, 5, 5);
		gbc_dbURLLb.gridx = 0;
		gbc_dbURLLb.gridy = 10;
		add(dbURLLb, gbc_dbURLLb);

		dbURLTextField = new JTextField();
		dbURLTextField.setEditable(false);
		GridBagConstraints gbc_dbURLTextField = new GridBagConstraints();
		gbc_dbURLTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_dbURLTextField.anchor = GridBagConstraints.NORTH;
		gbc_dbURLTextField.insets = new Insets(0, 0, 5, 5);
		gbc_dbURLTextField.gridx = 2;
		gbc_dbURLTextField.gridy = 10;
		add(dbURLTextField, gbc_dbURLTextField);
		dbURLTextField.setColumns(10);

		customDriverCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (customDriverCheckBox.isSelected()) {
					customDriverBtn.setEnabled(true);
				} else {
					customDriverBtn.setEnabled(false);
				}
			}
		});
		GridBagConstraints gbc_customDriverCheckBox = new GridBagConstraints();
		gbc_customDriverCheckBox.anchor = GridBagConstraints.EAST;
		gbc_customDriverCheckBox.gridwidth = 2;
		gbc_customDriverCheckBox.insets = new Insets(0, 0, 5, 5);
		gbc_customDriverCheckBox.gridx = 0;
		gbc_customDriverCheckBox.gridy = 11;
		customDriverCheckBox.setOpaque(false);
		add(customDriverCheckBox, gbc_customDriverCheckBox);

		customDriverTextField.setEditable(false);
		GridBagConstraints gbc_customDriverTextField = new GridBagConstraints();
		gbc_customDriverTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_customDriverTextField.insets = new Insets(0, 0, 5, 5);
		gbc_customDriverTextField.gridx = 2;
		gbc_customDriverTextField.gridy = 11;
		add(customDriverTextField, gbc_customDriverTextField);
		customDriverTextField.setColumns(10);

		customDriverBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SelectFile selectFile = new SelectFile(
						JFileChooser.OPEN_DIALOG, true,
						JFileChooser.FILES_ONLY, "请选择数据库驱动", "选择",
						new FileNameExtensionFilter("数据库驱动（*.jar）", "jar"));
				selectFile.getFileChooser().setAcceptAllFileFilterUsed(false);
				selectFile.getFileChooser().setCurrentDirectory(
						new File(AppConstants.SYSTEM_PATH));
				File[] files = selectFile.getSelectedFiles();
				if (files != null) {
					StringBuffer str = new StringBuffer();
					for (int i = 0; i < files.length; i++) {
						str.append(files[i].getAbsolutePath());
						if (i < files.length - 1) {
							str.append(";");
						}
					}
					customDriverTextField.setText(str.toString());
				}
			}
		});

		customDriverBtn.setEnabled(false);
		GridBagConstraints gbc_customDriverBtn = new GridBagConstraints();
		gbc_customDriverBtn.anchor = GridBagConstraints.NORTHWEST;
		gbc_customDriverBtn.insets = new Insets(0, 0, 5, 0);
		gbc_customDriverBtn.gridx = 3;
		gbc_customDriverBtn.gridy = 11;
		add(customDriverBtn, gbc_customDriverBtn);

		JLabel dbDriverClassLb = new JLabel(
				"\u6570\u636E\u5E93\u9A71\u52A8\u7C7B\uFF1A");
		GridBagConstraints gbc_dbDriverClassLb = new GridBagConstraints();
		gbc_dbDriverClassLb.gridwidth = 2;
		gbc_dbDriverClassLb.anchor = GridBagConstraints.EAST;
		gbc_dbDriverClassLb.insets = new Insets(0, 0, 5, 5);
		gbc_dbDriverClassLb.gridx = 0;
		gbc_dbDriverClassLb.gridy = 12;
		add(dbDriverClassLb, gbc_dbDriverClassLb);

		dbDriverClassTextField = new JTextField();
		GridBagConstraints gbc_dbDriverClassTextField = new GridBagConstraints();
		gbc_dbDriverClassTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_dbDriverClassTextField.anchor = GridBagConstraints.NORTH;
		gbc_dbDriverClassTextField.insets = new Insets(0, 0, 5, 5);
		gbc_dbDriverClassTextField.gridx = 2;
		gbc_dbDriverClassTextField.gridy = 12;
		add(dbDriverClassTextField, gbc_dbDriverClassTextField);
		dbDriverClassTextField.setColumns(10);

		GridBagConstraints gbc_initDBCheckBox = new GridBagConstraints();
		gbc_initDBCheckBox.anchor = GridBagConstraints.EAST;
		gbc_initDBCheckBox.insets = new Insets(0, 0, 5, 5);
		gbc_initDBCheckBox.gridx = 1;
		gbc_initDBCheckBox.gridy = 13;
		initDBCheckBox.setOpaque(false);
		add(initDBCheckBox, gbc_initDBCheckBox);

		JButton dbTestBtn = new JButton(
				"\u6570\u636E\u5E93\u8FDE\u63A5\u6D4B\u8BD5");
		GridBagConstraints gbc_dbTestBtn = new GridBagConstraints();
		gbc_dbTestBtn.insets = new Insets(0, 0, 5, 5);
		gbc_dbTestBtn.gridx = 2;
		gbc_dbTestBtn.gridy = 14;
		add(dbTestBtn, gbc_dbTestBtn);

		dbTestBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (testDBConnect()) {
					JOptionPane.showMessageDialog(getParent(), "数据库测试成功");
				} else {
					JOptionPane.showMessageDialog(getParent(), testMsg);
				}
			}
		});

		initDB(OpEvent.InitEvent);
	}

	/**
	 * 设置背景图片
	 */
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		// int x = 0, y = 0;
		// java.net.URL imgURL = getClass().getResource("test.jpg");
		// // test.jpg是测试图片，与Demo.java放在同一目录下
		// ImageIcon icon = new ImageIcon(imgURL);
		// g.drawImage(icon.getImage(), x, y, getSize().width, getSize().height,
		// this);
		// while (true) {
		// g.drawImage(icon.getImage(), x, y, this);
		// if (x > getSize().width && y > getSize().height)
		// break;
		// // 这段代码是为了保证在窗口大于图片时，图片仍能覆盖整个窗口
		// if (x > getSize().width) {
		// x = 0;
		// y += icon.getIconHeight();
		// } else {
		// x += icon.getIconWidth();
		// }
		// }
	}

	/**
	 * 初始化数据库信息
	 * 
	 * @param type
	 *            类型，0-页面初始化时加载，1-下拉改变时加载，2-文本框或数字框修改
	 */
	private void initDB(OpEvent type) {
		String selectedDbType = (String) dbTypeComboBox.getSelectedItem();
		IDBType dbType = DBFactory.createDB(selectedDbType);
		db.setDbType(selectedDbType);
		db.setOpType(type);
		db.setDbName(dbNameTextField.getText());
		db.setDbType(selectedDbType);
		db.setDriverClass(dbDriverClassTextField.getText());
		db.setHost(ipTextField.getText());
		db.setPassword(new String(dbPasswordField.getPassword()).trim());
		db.setUsername(dbUserTextField.getText());
		db.setPort((Integer) portSpinner.getValue());
		db.setCustomDriver(customDriverTextField.getText());
		db.setUrl(dbURLTextField.getText());
		dbType.fillDBInfo(db);
		customDriverTextField.setText(db.getCustomDriver());
		dbNameTextField.setText(db.getDbName());
		dbDriverClassTextField.setText(db.getDriverClass());
		ipTextField.setText(db.getHost());
		dbPasswordField.setText(db.getPassword());
		dbURLTextField.setText(db.getUrl());
		dbUserTextField.setText(db.getUsername());
		portSpinner.setValue(db.getPort());
		dbNameLb.setText(db.getDbNameTip());
	}

	/**
	 * 测试数据库连接
	 * 
	 * @return
	 */
	private boolean testDBConnect() {
		boolean flag = false;
		initDB(OpEvent.ModifyEvent);
		Connection conn = null;
		try {
			// 判断是否使用用户自选的jdbc驱动
			String[] drivers = null;
			if (customDriverCheckBox.isSelected()) {
				String customDriver = db.getCustomDriver();
				if (StringUtils.isNotEmpty(customDriver)) {
					drivers = customDriver.split(";");
				}
			} else {
				String defaultDriverJar = db.getDefaultDriverJar();
				if (StringUtils.isNotEmpty(defaultDriverJar)) {
					drivers = defaultDriverJar.split(";");
				}
			}
			URL[] urls = null;
			// 把字符串装换为URL
			if (drivers != null) {
				urls = new URL[drivers.length];
				for (int i = 0; i < drivers.length; i++) {
					urls[i] = new File(drivers[i]).toURI().toURL();
				}
			}
			DBUtils.loadClass(urls);
			// JarLoader.loadJar(urls);
			// 加载数据库驱动类
			// Class<?> loadClass = JarLoader.getClassLoader().loadClass(
			// db.getDriverClass());
			// 实例化数据库驱动对象
			// Driver driver = (Driver) loadClass.newInstance();
			// 获取Class所属的jar包
			// String path = loadClass.getProtectionDomain().getCodeSource()
			// .getLocation().getFile();
			// System.out.println(path);
			// 设置数据库连接的属性
			// Properties props = new Properties();
			// props.put("user", db.getUsername());
			// props.put("password", db.getPassword());
			// 获取数据库连接
			// conn = driver.connect(db.getUrl(), props);
			conn = DBUtils.getConnection(db.getUrl(), db.getUsername(),
					db.getPassword(), db.getDriverClass());
			flag = true;
		} catch (ClassNotFoundException e) {
			testMsg = "数据库驱动不正确";
		} catch (SQLException e) {
			testMsg = "数据库连接不成功";
		} catch (MalformedURLException e) {
			testMsg = "未找到数据库驱动的JAR包";
		} catch (Exception e) {
			testMsg = "未找到数据库驱动的JAR包";
		} finally {
			if (conn != null) {
				try {
					conn.close();
					flag = true;
				} catch (SQLException e) {
					conn = null;
					flag = false;
				}
			}
		}
		return flag;
	}

	@Override
	public PageResult validateInput() {
		boolean isNeeded = (Boolean) contextInst
				.getAttribute(AppConstants.DB_CFG_FLAG);
		boolean canConnect = testDBConnect();
		if (isNeeded && (!canConnect)) {
			return new PageResult(false, "请正确配置数据库信息");
		}
		if (initDBCheckBox.isSelected()) {
			return new PageResult(canConnect, testMsg);
		}
		return new PageResult(true);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected List<CustomConfig> createCustomConfig() {
		CustomConfig config = new CustomConfig();
		config.setType("数据库信息");
		CustomConfigItem dbType = new CustomConfigItem();
		dbType.setKey("数据库类型");
		dbType.setValue((String) dbTypeComboBox.getSelectedItem());
		config.addItem(dbType);

		CustomConfigItem serverIp = new CustomConfigItem();
		serverIp.setKey("服务器IP");
		serverIp.setValue(ipTextField.getText());
		config.addItem(serverIp);

		CustomConfigItem serverPort = new CustomConfigItem();
		serverPort.setKey("服务器端口");
		serverPort.setValue(portSpinner.getValue() + "");
		config.addItem(serverPort);

		CustomConfigItem dbName = new CustomConfigItem();
		dbName.setKey("数据库名称");
		dbName.setValue(dbNameTextField.getText());
		config.addItem(dbName);

		CustomConfigItem dbUser = new CustomConfigItem();
		dbUser.setKey("数据库用户");
		dbUser.setValue(dbUserTextField.getText());
		config.addItem(dbUser);

		CustomConfigItem dbPwd = new CustomConfigItem();
		dbPwd.setKey("数据库密码");
		dbPwd.setValue(new String(dbPasswordField.getPassword()));
		dbPwd.setVisible(false);
		config.addItem(dbPwd);

		CustomConfigItem dbUrl = new CustomConfigItem();
		dbUrl.setKey("数据库URL");
		dbUrl.setValue(dbURLTextField.getText());
		config.addItem(dbUrl);

		CustomConfigItem isCustomDriver = new CustomConfigItem();
		isCustomDriver.setKey("是否自选数据库驱动");
		isCustomDriver.setValue(customDriverCheckBox.isSelected() ? AppConstants.TRUE_FLAG
				: AppConstants.FALSE_FLAG);
		isCustomDriver.setVisible(false);
		config.addItem(isCustomDriver);

		CustomConfigItem dbCustomDriver = new CustomConfigItem();
		dbCustomDriver.setKey("数据库驱动");
		dbCustomDriver.setValue(customDriverTextField.getText());
		dbCustomDriver.setVisible(false);
		config.addItem(dbCustomDriver);

		CustomConfigItem driverClass = new CustomConfigItem();
		driverClass.setKey("数据库驱动类");
		driverClass.setValue(dbDriverClassTextField.getText());
		driverClass.setVisible(false);
		config.addItem(driverClass);

		CustomConfigItem initDb = new CustomConfigItem();
		initDb.setKey("是否初始化数据库");
		initDb.setValue(initDBCheckBox.isSelected() ? AppConstants.TRUE_FLAG
				: AppConstants.FALSE_FLAG);
		config.addItem(initDb);
		
		List<CustomConfig> configs = new ArrayList<CustomConfig>();
		configs.add(config);

		// 保存配置系统到系统上下文
		Map<String, CustomConfig> customCfgMap = (Map<String, CustomConfig>) contextInst
				.getAttribute(AppConstants.CUSTOM_CONFIG_MAP_KEY);
		customCfgMap.put(this.getClass().getName(), config);

		// 设置数据库初始化的数据信息
		contextInst.setAttribute(AppConstants.DB_URL, dbURLTextField.getText());
		contextInst.setAttribute(AppConstants.DB_USER,
				dbUserTextField.getText());
		contextInst.setAttribute(AppConstants.DB_PWD, new String(
				dbPasswordField.getPassword()));
		contextInst.setAttribute(AppConstants.DB_DRIVER_CLASS,
				dbDriverClassTextField.getText());
		if (customDriverCheckBox.isSelected()) {
			contextInst.setAttribute(AppConstants.DB_DRIVER_JAR,
					db.getCustomDriver());
		} else {
			contextInst.setAttribute(AppConstants.DB_DRIVER_JAR,
					db.getDefaultDriverJar());
		}
		contextInst.setAttribute(AppConstants.DB_INIT_FLAG,
				initDBCheckBox.isSelected());
		contextInst.setAttribute(AppConstants.DB_TYPE,
				(String) dbTypeComboBox.getSelectedItem());
		return configs;
	}
}
